package org.bronycorps.mlptss.domain.actor;

import org.bronycorps.mlptss.domain.controller.AbstractController;
import org.bronycorps.mlptss.domain.controller.BulletController;
import org.bronycorps.mlptss.domain.model.AbstractActorModel;
import org.bronycorps.mlptss.domain.model.Bullet;
import org.bronycorps.mlptss.manager.preferences.GameConfiguration;
import org.bronycorps.mlptss.screen.LevelScreen;
import org.bronycorps.mlptss.utils.AnimationCache;
import org.bronycorps.mlptss.utils.DrawableAnimation;

import com.badlogic.gdx.math.Vector2;

public class BulletActor extends AbstractActor {
	private boolean alive = true;
	private BulletController bulletController;
	private DrawableAnimation defaultAnimation;

	public BulletActor(BulletController bulletController) {
		super();

		this.bulletController = bulletController;

		createAnimation();
		this.currentAnimation = this.defaultAnimation;

		setDrawable(this.currentAnimation.getDrawableKeyFrame(this.currentTime));
		setWidth(getPrefWidth());
		setHeight(getPrefHeight());
		setOrigin(getModelOrigin().x, getModelOrigin().y);

		this.hitBox.setWidth(getBounds().width);
		this.hitBox.setHeight(getBounds().height);

	}

	@Override
	public void act(float delta) {
		super.act(delta);

		if (!this.alive || getX() > LevelScreen.STAGE_WIDTH + 10) {
			terminate();
		}
	}

	private void createAnimation() {

		String bulletTypeName = ((Bullet) getModel()).getOwner().getCharacter()
				.getBulletTypeName();

		String animName = GameConfiguration.getInstance().getCurrentMod()
				.getAssetsConfiguration().getBulletByName(bulletTypeName)
				.getAnimationName();
		this.defaultAnimation = AnimationCache.getInstance().get(animName);
	}

	public BulletController getBullet() {
		return this.bulletController;
	}

	@Override
	protected AbstractController getController() {
		return this.bulletController;
	}

	@Override
	public AbstractActorModel getModel() {
		return this.bulletController.getModel();
	}

	@Override
	protected Vector2 getModelPosition() {
		return getModel().getPosition();
	}

	public boolean isAlive() {
		return this.alive;
	}

	public void setAlive(boolean alive) {
		this.alive = alive;
	}

	public void setInitialPosition(Vector2 position) {
		getModelPosition().set(position);
		updateVisualPosition();
	}

	public void terminate() {
		getTrack().removeBullet(this);
	}

}
